package com.yswy.app.service.impl;

import com.alipay.api.domain.PeriodRuleParams;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.app.dto.VipPageResVo;
import com.yswy.app.service.*;
import com.yswy.common.enums.DateUnitEnums;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.DateUtils;
import com.yswy.domain.entity.AppUser;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.VipPrice;
import com.yswy.domain.mapper.PackageBingVipMapper;
import com.yswy.domain.mapper.VipPriceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class VipPriceServiceImpl extends ServiceImpl<VipPriceMapper, VipPrice>
        implements VipPriceService {

    @Autowired
    private SysConfigService configService ;
    @Autowired
    private PayOrderService payOrderService ;
    @Autowired
    private PackageBingVipMapper packageBingVipMapper ;
    @Autowired
    private PackageManageService packageManageService ;
    @Override
    public ResultModel getVipPage(AppUser appUser) {
        //判断是否购买套餐
        boolean isPay = payOrderService.isPayOrder(appUser.getId());
        List<VipPrice> vipPrices =packageBingVipMapper.getByPackageId(appUser.getPackageManageId() ,isPay) ;
        VipPageResVo vipPage = new VipPageResVo(vipPrices);
        return ResultModel.ok(vipPage);
    }

    @Override
    public VipPrice getPeriodRuleParamsByVip(Long id , Long userId) {
        //判断是否可以购买
        boolean isPay = payOrderService.isPayOrder(userId);
        VipPrice vipPrice = getById(id);
        Assert.notNull(vipPrice , "购买套餐不存在");
        if (isPay && vipPrice.getIsDisposable()){
            throw new ServiceException("体验次数已用完");
        }

        if (vipPrice.getIsWithdrawal()){
            Long totalPayments = vipPrice.getAgreemenPayments() ;
            Double total_amount = vipPrice.getAgreemenPrice().doubleValue()/100;
            Double total_payments = total_amount*totalPayments;
            String executeTime = null ;
            PeriodRuleParams periodRuleParams = new PeriodRuleParams();
            Date vipDateNow = new Date();
            if (vipPrice.getAgreemenTimeType().equals(DateUnitEnums.DAY.getMessage())){
                periodRuleParams.setPeriodType(DateUnitEnums.DAY.getMessage());
                periodRuleParams.setPeriod(vipPrice.getAgreemenTime());
            }else if (vipPrice.getAgreemenTimeType().equals(DateUnitEnums.MONTH.getMessage())){
                periodRuleParams.setPeriodType(DateUnitEnums.MONTH.getMessage());
                periodRuleParams.setPeriod(vipPrice.getAgreemenTime());
            }else if (vipPrice.getAgreemenTimeType().equals(DateUnitEnums.YEAR.getMessage())){
                periodRuleParams.setPeriodType(DateUnitEnums.MONTH.getMessage());
                periodRuleParams.setPeriod(vipPrice.getAgreemenTime()*12);
            }
//            if (vipPrice.getIsDisposable()){
//                vipDateNow = DateUtils.addDateNum(vipDateNow,DateUnitEnums.DAY.getMessage(),vipPrice.getVipTime().intValue());
//            }else {
//                vipDateNow = DateUtils.addDateNum(vipDateNow,vipPrice.getAgreemenTimeType(),vipPrice.getAgreemenTime().intValue());
//            }
            vipDateNow = DateUtils.addDateNum(vipDateNow,vipPrice.getVipTimeType(),vipPrice.getVipTime().intValue());
            if (vipPrice.getVipTimeType().equals("")){

            }
            executeTime = getExecuteTime(vipDateNow ,periodRuleParams.getPeriodType() );


            periodRuleParams.setExecuteTime(executeTime);
            periodRuleParams.setSingleAmount(total_amount.toString());
            periodRuleParams.setTotalAmount(total_payments.toString());
            periodRuleParams.setTotalPayments(totalPayments);
            vipPrice.setPeriodRuleParams(periodRuleParams);

        }
        return vipPrice;
    }

    public String getExecuteTime( Date vipDateNow ,String dateUnit ){
        Calendar calendar =  Calendar.getInstance();
        calendar.setTime(vipDateNow);
        Integer dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        if (dateUnit.equals(DateUnitEnums.MONTH.getMessage())){
            if (dayOfMonth > 28){
                vipDateNow = DateUtils.addDateNum(vipDateNow,DateUnitEnums.DAY.getMessage(), -dayOfMonth+1);
                vipDateNow = DateUtils.addDateNum(vipDateNow,DateUnitEnums.MONTH.getMessage(), 1);
            }
        }

        String executeTime = DateUtils.getSimpleDateFormat(DateUtils.YYYY_MM_DD).format(vipDateNow);
        return executeTime ;
    }



    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat simpleDateFormat = DateUtils.getSimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
//        Date date = simpleDateFormat.parse("2023-05-32 00:00:00");
//
//        Calendar calendar =  Calendar.getInstance();
//        calendar.setTime(date);
//        Integer dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
//        System.out.println(dayOfMonth);
//        if (dayOfMonth > 28){
//            date = DateUtils.addDateNum(date,DateUnitEnums.DAY.getMessage(), 28-dayOfMonth);
//        }
//        System.out.println(simpleDateFormat.format(date));
        SimpleDateFormat simpleDateFormat = DateUtils.getSimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
        Date date= new Date();
        Calendar calendar =  Calendar.getInstance();
        calendar.setTime(date);
        Integer dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(dayOfMonth);
        date = DateUtils.addDateNum(date,DateUnitEnums.DAY.getMessage(), -dayOfMonth+1);
        System.out.println(simpleDateFormat.format(date));
        date = DateUtils.addDateNum(date,DateUnitEnums.MONTH.getMessage(), 1);


        System.out.println(simpleDateFormat.format(date));
    }
}
